import pygame
import matrix
import inputhandler
from pygame.locals import *


screenx = 800
screeny = 600

mx = screenx/2.0
my = screeny/2.0

fokus = 200

class face: 
    polys = [] 
    z = 0 
    def __init__(self,polys): 
        a = 0 
        for i in polys: 
            a +=1 
            if a!=4: 
                self.polys.append(i) 
                self.z += i.z 
        self.z /=3 
        
    def move(self,x2,y2,z2): 
        self.z +=z2 
        #print self.z 
        polynew = [] 
        for i in self.polys: 
            x = i.x 
            y = i.y 
            z = i.z 
            x,y,z = matrix.move(x, y, z, x2,y2,z2) 
            curr = poly3d(x,y,z) 
            polynew.append(curr) 
        self.polys = polynew 
    def draw(self,col): 
        #print "drawing"
        poly2d = [] 
        for i in self.polys: 
            poly2d.append(Poly3dTo2d(i)) 
        pygame.draw.polygon(screen,col,poly2d,0) 
    def wire(self,col): 
        #print "wire face" 
        poly2d = [] 
        for i in self.polys: 
            poly2d.append(Poly3dTo2d(i)) 
        pygame.draw.polygon(screen,col,poly2d,1)

def Poly3dTo2d(poly): 
    x,y,z = poly.x,poly.y,poly.z 
    x,y,z = float(x),float(y),float(z) 
    if z==0.0: 
        z=-0.1 
    #print fokus,z 
    sf = fokus/z 
    x = x*sf 
    y = y*sf 

    return PosFromMiddle(x,y)

def PosFromMiddle(x,y):
    #x *=-1
    y *=-1
    return (mx+x,my+y)

class poly3d():
    x=0
    y=0
    z=0
    def __init__(self,x,y,z):
        self.x,self.y,self.z = x,y,z

    
class object:
    polys = []
    x=0
    y=0
    z=0
    def __init__(self,polys):
        for i in polys:
            self.polys.append(i)
    def move(self,x2,y2,z2):
        self.x +=x2
        self.y +=y2
        self.z +=z2
        polynew = []
        for i in self.polys:
            x = i.x
            y = i.y
            z = i.z
            x,y,z = matrix.move(x, y, z, x2,y2,z2)
            curr = poly3d(x,y,z)
            polynew.append(curr)
        self.polys = polynew
    def draw(self,col):
        poly2d = []
        for i in self.polys:
            poly2d.append(Poly3dTo2d(i))
        pygame.draw.polygon(screen,col,poly2d)
    def wire(self,col):
        poly2d = []
        for i in self.polys:
            poly2d.append(Poly3dTo2d(i))
        pygame.draw.polygon(screen,col,poly2d,1)
        
class obj:
    file = ""
    verts = []
    faces = []
    def __init__(self,file):
        self.file = file
    def load(self):
        f = open(self.file,"r")
        fcontent = f.readlines()
        for line in fcontent:
            l = line.split()
            if l[0] == "v":
                x,y,z = float(l[1]),float(l[2]),float(l[3])+200
                poly = poly3d(x,y,z)
                self.verts.append(poly)
            elif l[0] == "f":
                p1,p2,p3 = int(l[1])-1,int(l[2])-1,int(l[3])-1
                poly1,poly2,poly3 = self.verts[p1],self.verts[p2],self.verts[p3]
                currface = face((poly1,poly2,poly3))
                self.faces.append(currface)
        return 1
    def draw(self,col):
        for i in self.faces:
            #print "drawing face"
            #print i
            i.draw(col)
    def move(self,x,y,z):
        for i in self.faces:
            i.move(x,y,z)

#rect = (PosFromMiddle(-50,-50),
#        PosFromMiddle(50,-50),
#        PosFromMiddle(50,50),
#        PosFromMiddle(-50,50))

point1 = poly3d(50,50,200)
point2 = poly3d(-100,100,200)
point3 = poly3d(-100,-100,200)
rect = (point1,point2,point3)

test = face(rect)
#test2 = face(rect)
#test2.move(100, 0,0)
#test3 = object.face(rect)
#test3.move(50, 100,0)

#===============================================================================
# v 110.000000 -110.000008 -109.999992
# v 110.000000 -109.999992 110.000008
# v -110.000015 -109.999992 109.999985
# v -109.999954 -110.000008 -110.000031
# v 110.000053 109.999992 -109.999954
# v 109.999931 110.000008 110.000061
# v -110.000038 110.000008 109.999954
# v -109.999985 109.999992 -110.000008
#===============================================================================


v1 = poly3d(110,-110,-110)
v2 = poly3d(110,-110,110)
v3 = poly3d(-110,-110,110)
v4 = poly3d(-110,-110,-110)
v5 = poly3d(110,110,-110)
v6 = poly3d(110,110,110)
v7 = poly3d(-110,110,110)
v8 = poly3d(-110,110,-110)

#===============================================================================
# f 5 1 4
# f 5 4 8
# f 3 7 8
# f 3 8 4
# f 2 6 3
# f 6 7 3
# f 1 5 2
# f 5 6 2
# f 5 8 6
# f 8 7 6
# f 1 2 3
# f 1 3 4
#===============================================================================

f1 = face((v5,v1,v4))
f2 = face((v5,v3,v8))
f3 = face((v3,v7,v8))
f4 = face((v3,v8,v4))
f5 = face((v2,v6,v3))
f6 = face((v6,v7,v3))
f7 = face((v1,v5,v2))
f8 = face((v5,v6,v2))
f9 = face((v5,v8,v6))
f10 = face((v5,v7,v6))
f11 = face((v1,v2,v3))
f12 = face((v1,v3,v4))

faces = [f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12]
#objekt = obj("ball.obj")
#objekt.load()
#faces = objekt.faces

#obj2 = object.object(objekt.faces)

#objects = []
#for i in range(0,1):
#    a = face(rect)
#    a.move(10*i,0,0)
#    objects.append(a)
    
#print test.z

fi = open("cube.obj")
cv = 1
cf = 1
v = {}
f = []
for i in fi:
    #vert=[]
    #face=[]
    i = i.split()
    if i[0]=="v":
        vert = poly3d(float(i[1]),float(i[2]),float(i[3]))
        #vert.append(float(i[1]))
        #vert.append(float(i[2]))
        #vert.append(float(i[3]))
        #print vert
        v[cv] = vert
        cv +=1
    elif i[0] == "f":
        #face.append(v[int(i[1])])
        #face.append(v[int(i[2])])
        #face.append(v[int(i[3])])
        #print face
        list = (v[int(i[1])],
                 v[int(i[2])],
                 v[int(i[3])])
        fa = face((v[int(i[1])],
                 v[int(i[2])],
                 v[int(i[3])]))
        fa.draw()
        print fa
        f.append(fa)
        cf +=1
    else:
        pass




pygame.init()

window = pygame.display.set_mode((screenx,screeny))

screen = pygame.display.get_surface() 

purple = pygame.Color("purple")
black = pygame.Color("black")

#test.draw(purple)

pygame.display.flip()
scrfill = pygame.Rect(0,0,screenx,screeny)
quit = 0
while not quit:
    #objekt.draw(purple)
    
    clock = pygame.time.Clock()
    #print clock.get_time()

    pygame.Surface.fill(screen,black)
    #test.draw(purple)
    mox = 0
    moy = 0
    moz = 0
    quit = inputhandler.inputhandler(pygame.event.get())
    if inputhandler.keys.has_key("w"):
        moy +=1
    if inputhandler.keys.has_key("s"):
        moy -=1
    if inputhandler.keys.has_key("a"):
        mox -=1
    if inputhandler.keys.has_key("d"):
        mox +=1
    if inputhandler.keys.has_key("shift"):
        moz +=1
    if inputhandler.keys.has_key("alt"):
        moz -=1
        
    for i in f:
        f.draw(purple)
    #for i in faces:
    #    i.draw(purple)
    pygame.Surface.fill(screen,black)
    #objekt.move(mox,moy,moz)
    test.draw(purple)
    test.move(mox, moy, moz)
    #objekt.draw(purple)
    #test.move(mox, moy, moz)
    #obj2.draw(purple)
    #obj2.move(mox,moy,moz)
        
    pygame.display.flip()